import {post} from '../../../utils/request.js'
import global from '../../../utils/global.js'
let that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listUser: [],
    userId: wx.getStorageSync("useInfo").id,
    page: 1,
    height: '',
    isSelect:false,
    // d_state:"",
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    that = this;
    wx.getSystemInfo({
      success: (res) => {
        that.setData({
          height: res.windowHeight - 50
        })
      }
    })   
    if (options.select == "1"){
      that.setData({
        isSelect:true
      })
    } 
   
  },
  onShow:function(){
    that.setData({
      page:1,
      listUser:[]
    })
    console.log(that.data.isFist) 
    
    that.getList(that.data.page)
  },
  // 获取体检报告
  getList(page) {
    let param = {
      page: that.data.page++,
      userId:that.data.userId
      // userId: "1738"
    }
    post(global.apiUrl.list, param, function(res) {
      console.log(res.obj)
      if (!res.more) {
        wx.showToast({
          title: '已经加载完数据',
          icon: 'loading',
          duration: 1000
        })

        return
      }
      let curArr = that.data.listUser.concat(res.obj)
      that.setData({
        listUser: curArr
      })

    })
  },
  // 去新增体检人或者修改体检人信息
  updateUser: function(item) {
    console.log(item,"去天津")
    console.log(!item.target.dataset.item)
    let url;
    if (!item.target.dataset.item) {
      url = '/pages/userCenter/regularChecker/updateMedicalUser/updateMedicalUser?item=1'
    } else {
      url = '/pages/userCenter/regularChecker/updateMedicalUser/updateMedicalUser?item=' + JSON.stringify(item.target.dataset.item)
    }

    wx.navigateTo({
      url: url
    })
  },
  //删除体检人
  deleteUser: function(e) {
    console.log(e.currentTarget.dataset.id)
    wx.showModal({
      title: '提示',
      content: '您确定要删除该体检人？',
      success: function(res) {
        console.log(res.confirm)
        if (res.confirm) {
          post(global.apiUrl.deleteUser, {id: e.currentTarget.dataset.id}, function(res) {
            that.onShow();

          })


        }

      }
    })

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    // 停止下拉动作
    wx.stopPullDownRefresh();

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  lower: function() {
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    // 页数+1
    that.getList(that.data.page)
  },
  //选择
  // selectYes(e){
  //   console.log(e.currentTarget.dataset.index)
  //   console.log(that.data.index)
  //   that.setData()
   
  // }
  selectYes: function (e) {
    console.log(e.currentTarget.dataset.item)
    if (!that.data.isSelect){
      return
    }
    wx.setStorageSync("selectItem", e.currentTarget.dataset.item)
    that.setData({
      d_state: e.currentTarget.dataset.key,
    });
    wx
    wx.showToast({
      title: '已选择',
      icon: 'succes',
      duration: 1000,
      mask: true
    })
    setTimeout(function () {
      wx.navigateBack({
        delta: 1
      })
    }, 1000)
  }
})